import * as React from 'react';
import { useBusinessList } from '../../hook/businessList';
import './index.less';
import { Card, Icon } from 'antd';

import InfoCard from '../../component/info-card';

const { useState, useEffect } = React;

export interface OneDayStatics {
    badjscount: number,
    badjsid: number,
    date: number,
    id: number,
    pv: number,
    rate: string
}
export interface OverviewProps {
    overview: {
        overview1: OneDayStatics[],
        overview2: OneDayStatics[]
    },
    loading: boolean
}

export default function (props: OverviewProps) {
    const { overview1 = [], overview2 = [] } = props.overview;
    const loading = props.loading;
    let pvCounter = 0, errorCounter = 0, pvCounter2DaysAgo = 0, errorCounter2DaysAgo = 0;

    const unActiveProjects = [];
    for (let i = 0; i < overview1.length; i++) {
        if (overview1[i]) {
            pvCounter += overview1[i].pv;
            errorCounter += overview1[i].badjscount;
        }
        if (overview2[i]) {
            pvCounter2DaysAgo += overview2[i].pv;
            errorCounter2DaysAgo += overview2[i].badjscount;
        }
        // 不活跃的项目
        if (overview1[i].pv < 200 || overview1[i].badjscount < 10) {
            unActiveProjects.push(overview1[i].badjsid);
        }
    }
    localStorage.setItem('unActiveProjects', unActiveProjects.join(','))
    let pvCompare = 0;
    if (pvCounter !== 0) {
        pvCompare = (pvCounter - pvCounter2DaysAgo) / pvCounter;
    }
    let errorCompare = 0;
    if (errorCounter !== 0) {
        errorCompare = (errorCounter - errorCounter2DaysAgo) / errorCounter;
    }
    const percent1day = errorCounter / pvCounter || 0;
    const percent2day = errorCounter2DaysAgo / pvCounter2DaysAgo || 0;
    let percentCompare = 0;
    if (percent1day !== 0) {
        percentCompare = (percent1day - percent2day) / percent1day;
    }
    return (
        <div className="all-overview">
            <h3>昨日数据概览</h3>
            <div className="cards-wrap">
                <InfoCard loading={loading} fixedNum={0} title='总PV' compareNum={pvCompare} num={pvCounter}></InfoCard>
                <InfoCard loading={loading} fixedNum={0} title='总错误量' compareNum={errorCompare} num={errorCounter}></InfoCard>
                <InfoCard loading={loading} title='总错误量/总PV' compareNum={percentCompare} num={(percent1day * 100).toFixed(4) + '%'}></InfoCard>
            </div >
        </div >
    );
}